/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Orders.java
 *
 * Created on 8-dec-2011, 13:27:19
 */
package view;

import connectivity.Dbmanager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Order;

/**
 *
 * @author Bryan
 */
public class Orders extends javax.swing.JPanel {
    private DefaultTableModel modelOrder;
    private List<Order> orders;
    private boolean menu;
    /** Creates new form Orders */
    public Orders(boolean menu, int customerID) {
        WinkelApplication.mainWindow.setTitle("Orders");
        initComponents();
        this.menu = menu;
        List<Order> orders = WinkelApplication.getQueryManager().getOrders(customerID);
        modelOrder = (DefaultTableModel) this.tblOrders.getModel();
        
        for(Order order: orders) {
            String status;
            if(order.getStatus() == 0) status = "Openstaand";
            else if(order.getStatus() == 1) status = "Afgerond";
            else if(order.getStatus() == -1) status = "Geannuleerd";
            else status = "Niet bestaand!";
            
            
            modelOrder.addRow(new Object[] {new Integer(order.getOrderId()),
            order.getCustomer(),
            order.getDate(),
            status});
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblOrders = new javax.swing.JTable();
        btnView = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        tblOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Order id", "Klant", "Datum", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblOrders);

        btnView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgSelect.gif"))); // NOI18N
        btnView.setText("Bekijk order");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png"))); // NOI18N
        btnBack.setText("Terug");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 190, Short.MAX_VALUE)
                        .addComponent(btnView)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnView)
                    .addComponent(btnBack))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        if(tblOrders.getSelectedRow() != -1) {
            int orderId = Integer.parseInt(modelOrder.getValueAt(tblOrders.getSelectedRow(),0).toString());
            WinkelApplication.getInstance().showPanel(new ViewOrder(orderId,this.menu));
        } else WinkelApplication.msgbox("Je moet een order selecteren!");
    }//GEN-LAST:event_btnViewActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        if(this.menu){
          WinkelApplication.getInstance().showPanel(new MainMenu());
        } else{
            WinkelApplication.getInstance().showPanel(new CustomerOverview(false));
        }
    }//GEN-LAST:event_btnBackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblOrders;
    // End of variables declaration//GEN-END:variables
}
